﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using MRCPSP.Controllers;
using MRCPSP.Algorithm;
using MRCPSP.Algorithm.FirstGeneration;
using MRCPSP.Algorithm.SelectionPolicy;
using MRCPSP.Algorithm.CrossOver;

namespace MRCPSP.Gui.ProblemSolver
{
    class ProblemSolverMonitor : Form
    {
        private Label label1;
        private Button m_start_test_button;
        private ListBox m_all_problems_lst;
        private Button m_refresh_list_button;
        private TableLayoutPanel tableLayoutPanel1;
        private Panel panel1;
        private ListBox m_selected_problems_lst;
        private Button m_clear_selected_button;
        private Label label2;
        private GroupBox groupBox1;
        private TextBox m_mutation_percent_le;
        private TextBox m_num_of_gen_le;
        private TextBox m_population_size_le;
        private Label label5;
        private Label label4;
        private Label label3;
        private NumericUpDown m_loops_sb;
        private Label label6;
        private Button m_stop_btn;
        private Panel panel2;
        private Panel panel3;
        private Button m_remove_problem_button;
        private Button m_add_problem_button;
        private RadioButton m_solve_current_rb;
        private RadioButton m_use_table_rb;
        private GroupBox groupBox3;
        private Panel m_list_panel;
        private GroupBox groupBox4;
        private RadioButton m_pop_by_operation_rb;
        private RadioButton m_first_pop_by_resource_rb;
        private GroupBox groupBox5;
        private RadioButton m_rank_rb;
        private RadioButton m_score_rb;
        private Label label7;
        private NumericUpDown m_elitisem_sb;
        private RadioButton m_roulette_wheel_rb;
        private Label label8;

        private BackgroundWorker m_background_worker;

        public ProblemSolverMonitor()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.m_all_problems_lst = new System.Windows.Forms.ListBox();
            this.m_refresh_list_button = new System.Windows.Forms.Button();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.m_selected_problems_lst = new System.Windows.Forms.ListBox();
            this.m_clear_selected_button = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel3 = new System.Windows.Forms.Panel();
            this.m_remove_problem_button = new System.Windows.Forms.Button();
            this.m_add_problem_button = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.m_elitisem_sb = new System.Windows.Forms.NumericUpDown();
            this.label6 = new System.Windows.Forms.Label();
            this.m_loops_sb = new System.Windows.Forms.NumericUpDown();
            this.m_mutation_percent_le = new System.Windows.Forms.TextBox();
            this.m_num_of_gen_le = new System.Windows.Forms.TextBox();
            this.m_population_size_le = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.m_solve_current_rb = new System.Windows.Forms.RadioButton();
            this.m_use_table_rb = new System.Windows.Forms.RadioButton();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.m_list_panel = new System.Windows.Forms.Panel();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.m_pop_by_operation_rb = new System.Windows.Forms.RadioButton();
            this.m_first_pop_by_resource_rb = new System.Windows.Forms.RadioButton();
            this.groupBox5 = new System.Windows.Forms.GroupBox();
            this.m_roulette_wheel_rb = new System.Windows.Forms.RadioButton();
            this.m_score_rb = new System.Windows.Forms.RadioButton();
            this.m_rank_rb = new System.Windows.Forms.RadioButton();
            this.m_stop_btn = new System.Windows.Forms.Button();
            this.m_start_test_button = new System.Windows.Forms.Button();
            this.tableLayoutPanel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.panel3.SuspendLayout();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.m_elitisem_sb)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.m_loops_sb)).BeginInit();
            this.groupBox3.SuspendLayout();
            this.m_list_panel.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.groupBox5.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Dock = System.Windows.Forms.DockStyle.Top;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(126, 17);
            this.label1.TabIndex = 2;
            this.label1.Text = "available problems";
            // 
            // m_all_problems_lst
            // 
            this.m_all_problems_lst.Dock = System.Windows.Forms.DockStyle.Fill;
            this.m_all_problems_lst.FormattingEnabled = true;
            this.m_all_problems_lst.ItemHeight = 16;
            this.m_all_problems_lst.Location = new System.Drawing.Point(0, 17);
            this.m_all_problems_lst.Name = "m_all_problems_lst";
            this.m_all_problems_lst.Size = new System.Drawing.Size(202, 164);
            this.m_all_problems_lst.TabIndex = 4;
            // 
            // m_refresh_list_button
            // 
            this.m_refresh_list_button.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.m_refresh_list_button.Location = new System.Drawing.Point(0, 195);
            this.m_refresh_list_button.Name = "m_refresh_list_button";
            this.m_refresh_list_button.Size = new System.Drawing.Size(202, 23);
            this.m_refresh_list_button.TabIndex = 7;
            this.m_refresh_list_button.Text = "refresh";
            this.m_refresh_list_button.UseVisualStyleBackColor = true;
            this.m_refresh_list_button.Click += new System.EventHandler(this.m_refresh_list_button_Click);
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.BackColor = System.Drawing.Color.Transparent;
            this.tableLayoutPanel1.ColumnCount = 3;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 70F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Controls.Add(this.panel2, 2, 0);
            this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.panel3, 1, 0);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 1;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(486, 224);
            this.tableLayoutPanel1.TabIndex = 8;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.m_selected_problems_lst);
            this.panel2.Controls.Add(this.m_clear_selected_button);
            this.panel2.Controls.Add(this.label2);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Location = new System.Drawing.Point(281, 3);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(202, 218);
            this.panel2.TabIndex = 9;
            // 
            // m_selected_problems_lst
            // 
            this.m_selected_problems_lst.Dock = System.Windows.Forms.DockStyle.Fill;
            this.m_selected_problems_lst.FormattingEnabled = true;
            this.m_selected_problems_lst.ItemHeight = 16;
            this.m_selected_problems_lst.Location = new System.Drawing.Point(0, 17);
            this.m_selected_problems_lst.Name = "m_selected_problems_lst";
            this.m_selected_problems_lst.Size = new System.Drawing.Size(202, 164);
            this.m_selected_problems_lst.TabIndex = 10;
            // 
            // m_clear_selected_button
            // 
            this.m_clear_selected_button.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.m_clear_selected_button.Location = new System.Drawing.Point(0, 195);
            this.m_clear_selected_button.Name = "m_clear_selected_button";
            this.m_clear_selected_button.Size = new System.Drawing.Size(202, 23);
            this.m_clear_selected_button.TabIndex = 9;
            this.m_clear_selected_button.Text = "clear";
            this.m_clear_selected_button.UseVisualStyleBackColor = true;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Dock = System.Windows.Forms.DockStyle.Top;
            this.label2.Location = new System.Drawing.Point(0, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(123, 17);
            this.label2.TabIndex = 7;
            this.label2.Text = "selected problems";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.m_all_problems_lst);
            this.panel1.Controls.Add(this.m_refresh_list_button);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(3, 3);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(202, 218);
            this.panel1.TabIndex = 9;
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.m_remove_problem_button);
            this.panel3.Controls.Add(this.m_add_problem_button);
            this.panel3.Location = new System.Drawing.Point(211, 3);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(64, 218);
            this.panel3.TabIndex = 10;
            // 
            // m_remove_problem_button
            // 
            this.m_remove_problem_button.Image = global::MRCPSP.Properties.Resources.arrow_left;
            this.m_remove_problem_button.Location = new System.Drawing.Point(3, 98);
            this.m_remove_problem_button.Name = "m_remove_problem_button";
            this.m_remove_problem_button.Size = new System.Drawing.Size(58, 41);
            this.m_remove_problem_button.TabIndex = 13;
            this.m_remove_problem_button.UseVisualStyleBackColor = true;
            this.m_remove_problem_button.Click += new System.EventHandler(this.m_remove_problem_button_Click);
            // 
            // m_add_problem_button
            // 
            this.m_add_problem_button.Image = global::MRCPSP.Properties.Resources.arrow_right;
            this.m_add_problem_button.Location = new System.Drawing.Point(3, 53);
            this.m_add_problem_button.Name = "m_add_problem_button";
            this.m_add_problem_button.Size = new System.Drawing.Size(58, 39);
            this.m_add_problem_button.TabIndex = 12;
            this.m_add_problem_button.UseVisualStyleBackColor = true;
            this.m_add_problem_button.Click += new System.EventHandler(this.m_add_problem_button_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.BackColor = System.Drawing.Color.Transparent;
            this.groupBox1.Controls.Add(this.label8);
            this.groupBox1.Controls.Add(this.label7);
            this.groupBox1.Controls.Add(this.m_elitisem_sb);
            this.groupBox1.Controls.Add(this.label6);
            this.groupBox1.Controls.Add(this.m_loops_sb);
            this.groupBox1.Controls.Add(this.m_mutation_percent_le);
            this.groupBox1.Controls.Add(this.m_num_of_gen_le);
            this.groupBox1.Controls.Add(this.m_population_size_le);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            this.groupBox1.Location = new System.Drawing.Point(531, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(283, 213);
            this.groupBox1.TabIndex = 9;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Parameters";
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(15, 143);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(56, 17);
            this.label8.TabIndex = 8;
            this.label8.Text = "Elitisem";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(257, 145);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(20, 17);
            this.label7.TabIndex = 4;
            this.label7.Text = "%";
            // 
            // m_elitisem_sb
            // 
            this.m_elitisem_sb.Location = new System.Drawing.Point(177, 143);
            this.m_elitisem_sb.Name = "m_elitisem_sb";
            this.m_elitisem_sb.Size = new System.Drawing.Size(74, 23);
            this.m_elitisem_sb.TabIndex = 3;
            this.m_elitisem_sb.Value = new decimal(new int[] {
            10,
            0,
            0,
            0});
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label6.Location = new System.Drawing.Point(15, 176);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(51, 17);
            this.label6.TabIndex = 6;
            this.label6.Text = "Loops:";
            // 
            // m_loops_sb
            // 
            this.m_loops_sb.Location = new System.Drawing.Point(177, 174);
            this.m_loops_sb.Maximum = new decimal(new int[] {
            10000,
            0,
            0,
            0});
            this.m_loops_sb.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.m_loops_sb.Name = "m_loops_sb";
            this.m_loops_sb.Size = new System.Drawing.Size(100, 23);
            this.m_loops_sb.TabIndex = 7;
            this.m_loops_sb.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
            // 
            // m_mutation_percent_le
            // 
            this.m_mutation_percent_le.Location = new System.Drawing.Point(177, 106);
            this.m_mutation_percent_le.Name = "m_mutation_percent_le";
            this.m_mutation_percent_le.Size = new System.Drawing.Size(100, 23);
            this.m_mutation_percent_le.TabIndex = 5;
            this.m_mutation_percent_le.Text = "0";
            // 
            // m_num_of_gen_le
            // 
            this.m_num_of_gen_le.Location = new System.Drawing.Point(177, 73);
            this.m_num_of_gen_le.Name = "m_num_of_gen_le";
            this.m_num_of_gen_le.Size = new System.Drawing.Size(100, 23);
            this.m_num_of_gen_le.TabIndex = 4;
            this.m_num_of_gen_le.Text = "10";
            // 
            // m_population_size_le
            // 
            this.m_population_size_le.Location = new System.Drawing.Point(177, 33);
            this.m_population_size_le.Name = "m_population_size_le";
            this.m_population_size_le.Size = new System.Drawing.Size(100, 23);
            this.m_population_size_le.TabIndex = 3;
            this.m_population_size_le.Text = "10";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label5.Location = new System.Drawing.Point(15, 109);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(139, 17);
            this.label5.TabIndex = 2;
            this.label5.Text = "Mutation Percentage";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label4.Location = new System.Drawing.Point(15, 76);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(152, 17);
            this.label4.TabIndex = 1;
            this.label4.Text = "Number Of Generation";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label3.Location = new System.Drawing.Point(15, 36);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(106, 17);
            this.label3.TabIndex = 0;
            this.label3.Text = "Population Size";
            // 
            // m_solve_current_rb
            // 
            this.m_solve_current_rb.AutoSize = true;
            this.m_solve_current_rb.Checked = true;
            this.m_solve_current_rb.ForeColor = System.Drawing.SystemColors.ControlText;
            this.m_solve_current_rb.Location = new System.Drawing.Point(6, 19);
            this.m_solve_current_rb.Name = "m_solve_current_rb";
            this.m_solve_current_rb.Size = new System.Drawing.Size(112, 21);
            this.m_solve_current_rb.TabIndex = 12;
            this.m_solve_current_rb.TabStop = true;
            this.m_solve_current_rb.Text = "Solve Current";
            this.m_solve_current_rb.UseVisualStyleBackColor = true;
            this.m_solve_current_rb.CheckedChanged += new System.EventHandler(this.m_solve_current_rb_CheckedChanged);
            // 
            // m_use_table_rb
            // 
            this.m_use_table_rb.AutoSize = true;
            this.m_use_table_rb.ForeColor = System.Drawing.SystemColors.ControlText;
            this.m_use_table_rb.Location = new System.Drawing.Point(124, 19);
            this.m_use_table_rb.Name = "m_use_table_rb";
            this.m_use_table_rb.Size = new System.Drawing.Size(186, 21);
            this.m_use_table_rb.TabIndex = 13;
            this.m_use_table_rb.TabStop = true;
            this.m_use_table_rb.Text = "Solve Problems From List";
            this.m_use_table_rb.UseVisualStyleBackColor = true;
            this.m_use_table_rb.CheckedChanged += new System.EventHandler(this.m_use_table_rb_CheckedChanged);
            // 
            // groupBox3
            // 
            this.groupBox3.BackColor = System.Drawing.Color.Transparent;
            this.groupBox3.Controls.Add(this.m_list_panel);
            this.groupBox3.Controls.Add(this.m_solve_current_rb);
            this.groupBox3.Controls.Add(this.m_use_table_rb);
            this.groupBox3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            this.groupBox3.Location = new System.Drawing.Point(12, 12);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(498, 279);
            this.groupBox3.TabIndex = 14;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Problems Source";
            // 
            // m_list_panel
            // 
            this.m_list_panel.Controls.Add(this.tableLayoutPanel1);
            this.m_list_panel.Location = new System.Drawing.Point(6, 42);
            this.m_list_panel.Name = "m_list_panel";
            this.m_list_panel.Size = new System.Drawing.Size(486, 224);
            this.m_list_panel.TabIndex = 14;
            // 
            // groupBox4
            // 
            this.groupBox4.Controls.Add(this.m_pop_by_operation_rb);
            this.groupBox4.Controls.Add(this.m_first_pop_by_resource_rb);
            this.groupBox4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            this.groupBox4.Location = new System.Drawing.Point(238, 313);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(206, 106);
            this.groupBox4.TabIndex = 15;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "Create First Population";
            // 
            // m_pop_by_operation_rb
            // 
            this.m_pop_by_operation_rb.AutoSize = true;
            this.m_pop_by_operation_rb.Checked = true;
            this.m_pop_by_operation_rb.Location = new System.Drawing.Point(6, 19);
            this.m_pop_by_operation_rb.Name = "m_pop_by_operation_rb";
            this.m_pop_by_operation_rb.Size = new System.Drawing.Size(109, 21);
            this.m_pop_by_operation_rb.TabIndex = 1;
            this.m_pop_by_operation_rb.TabStop = true;
            this.m_pop_by_operation_rb.Text = "By Operation";
            this.m_pop_by_operation_rb.UseVisualStyleBackColor = true;
            // 
            // m_first_pop_by_resource_rb
            // 
            this.m_first_pop_by_resource_rb.AutoSize = true;
            this.m_first_pop_by_resource_rb.Location = new System.Drawing.Point(6, 45);
            this.m_first_pop_by_resource_rb.Name = "m_first_pop_by_resource_rb";
            this.m_first_pop_by_resource_rb.Size = new System.Drawing.Size(107, 21);
            this.m_first_pop_by_resource_rb.TabIndex = 0;
            this.m_first_pop_by_resource_rb.Text = "By Resource";
            this.m_first_pop_by_resource_rb.UseVisualStyleBackColor = true;
            // 
            // groupBox5
            // 
            this.groupBox5.Controls.Add(this.m_roulette_wheel_rb);
            this.groupBox5.Controls.Add(this.m_score_rb);
            this.groupBox5.Controls.Add(this.m_rank_rb);
            this.groupBox5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            this.groupBox5.Location = new System.Drawing.Point(12, 313);
            this.groupBox5.Name = "groupBox5";
            this.groupBox5.Size = new System.Drawing.Size(220, 106);
            this.groupBox5.TabIndex = 16;
            this.groupBox5.TabStop = false;
            this.groupBox5.Text = "Selection Policy";
            // 
            // m_roulette_wheel_rb
            // 
            this.m_roulette_wheel_rb.AutoSize = true;
            this.m_roulette_wheel_rb.Checked = true;
            this.m_roulette_wheel_rb.Location = new System.Drawing.Point(6, 45);
            this.m_roulette_wheel_rb.Name = "m_roulette_wheel_rb";
            this.m_roulette_wheel_rb.Size = new System.Drawing.Size(123, 21);
            this.m_roulette_wheel_rb.TabIndex = 5;
            this.m_roulette_wheel_rb.TabStop = true;
            this.m_roulette_wheel_rb.Text = "Roulette Wheel";
            this.m_roulette_wheel_rb.UseVisualStyleBackColor = true;
            // 
            // m_score_rb
            // 
            this.m_score_rb.AutoSize = true;
            this.m_score_rb.Location = new System.Drawing.Point(6, 72);
            this.m_score_rb.Name = "m_score_rb";
            this.m_score_rb.Size = new System.Drawing.Size(63, 21);
            this.m_score_rb.TabIndex = 2;
            this.m_score_rb.TabStop = true;
            this.m_score_rb.Text = "Score";
            this.m_score_rb.UseVisualStyleBackColor = true;
            // 
            // m_rank_rb
            // 
            this.m_rank_rb.AutoSize = true;
            this.m_rank_rb.Location = new System.Drawing.Point(6, 19);
            this.m_rank_rb.Name = "m_rank_rb";
            this.m_rank_rb.Size = new System.Drawing.Size(59, 21);
            this.m_rank_rb.TabIndex = 0;
            this.m_rank_rb.Text = "Rank";
            this.m_rank_rb.UseVisualStyleBackColor = true;
            // 
            // m_stop_btn
            // 
            this.m_stop_btn.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            this.m_stop_btn.Image = global::MRCPSP.Properties.Resources.stop;
            this.m_stop_btn.Location = new System.Drawing.Point(631, 313);
            this.m_stop_btn.Name = "m_stop_btn";
            this.m_stop_btn.Size = new System.Drawing.Size(87, 84);
            this.m_stop_btn.TabIndex = 11;
            this.m_stop_btn.UseVisualStyleBackColor = true;
            this.m_stop_btn.Click += new System.EventHandler(this.m_stop_btn_Click);
            // 
            // m_start_test_button
            // 
            this.m_start_test_button.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            this.m_start_test_button.Image = global::MRCPSP.Properties.Resources.play;
            this.m_start_test_button.Location = new System.Drawing.Point(724, 313);
            this.m_start_test_button.Name = "m_start_test_button";
            this.m_start_test_button.Size = new System.Drawing.Size(87, 84);
            this.m_start_test_button.TabIndex = 3;
            this.m_start_test_button.UseVisualStyleBackColor = true;
            this.m_start_test_button.Click += new System.EventHandler(this.m_start_test_button_Click);
            // 
            // ProblemSolverMonitor
            // 
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(826, 431);
            this.Controls.Add(this.groupBox5);
            this.Controls.Add(this.groupBox4);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.m_stop_btn);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.m_start_test_button);
            this.Name = "ProblemSolverMonitor";
            this.Text = "Problem Solver Monitor";
            this.Load += new System.EventHandler(this.ProblemSolverMonitor_Load);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.panel3.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.m_elitisem_sb)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.m_loops_sb)).EndInit();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.m_list_panel.ResumeLayout(false);
            this.groupBox4.ResumeLayout(false);
            this.groupBox4.PerformLayout();
            this.groupBox5.ResumeLayout(false);
            this.groupBox5.PerformLayout();
            this.ResumeLayout(false);

        }

        private void m_start_test_button_Click(object sender, EventArgs e)
        {
            if (m_use_table_rb.Checked)
            {
                if (this.m_selected_problems_lst.Items.Count == 0)
                {
                    MessageBox.Show("Please select problems first", "notify");
                    return;
                }
            }
            else
            {
                if (ApplicManager.Instance.CurrentProblem == null)
                {
                    MessageBox.Show("Please load problem first", "notify");
                    return;
                }
            }
            if (m_background_worker.IsBusy)
                return;

            SelectionPolicyBase selection = getSelectionPolicy();
            CorssOverBase crossover = getCrossOverPolicy();
            GeneratePolicyBase first = getFirstPopulationPolicy();

            ApplicManager.Instance.SolvingFromList = m_use_table_rb.Checked;
            ApplicManager.Instance.loadParams((int)m_loops_sb.Value, first, crossover, selection);

            if (m_solve_current_rb.Checked == false)                
                ApplicManager.Instance.loadProblemNames(m_selected_problems_lst.Items.Cast<String>().ToList<String>());
           
            int[] alg_params = new int[3];
            alg_params[0] = Convert.ToInt32(m_population_size_le.Text);
            alg_params[1] = Convert.ToInt32(m_num_of_gen_le.Text);
            alg_params[2] = Convert.ToInt32(m_mutation_percent_le.Text);
            m_background_worker.RunWorkerAsync(alg_params);
                                                   
        }

        private SelectionPolicyBase getSelectionPolicy()
        {
            if (m_rank_rb.Checked)
                return new RankSelectionPolicy((int)m_elitisem_sb.Value / 100.0);
            else if (m_roulette_wheel_rb.Checked)
                return new RouletteWheelPolicy((int)m_elitisem_sb.Value / 100.0);
            else
                return new ScoreSelectionPolicy();
        }


        private CorssOverBase getCrossOverPolicy()
        {
            return new OnePointCrossOver();
        }

        private GeneratePolicyBase getFirstPopulationPolicy()
        {
            if (m_pop_by_operation_rb.Checked)
                return new GenerateByOperation();
            else
                return new GenerateRandomPopulation();
        }


        private void m_stop_btn_Click(object sender, EventArgs e)
        {
            m_background_worker.CancelAsync();
        }

        public void signBackgroundWorker(BackgroundWorker worker)
        {
            m_background_worker = worker;

        }

        private void m_solve_current_rb_CheckedChanged(object sender, EventArgs e)
        {
            m_list_panel.Enabled = false;  
        }

        private void m_use_table_rb_CheckedChanged(object sender, EventArgs e)
        {
            m_list_panel.Enabled = true;
        }

        private void ProblemSolverMonitor_Load(object sender, EventArgs e)
        {
            m_list_panel.Enabled = ! m_solve_current_rb.Checked;

        }

        private void m_add_problem_button_Click(object sender, EventArgs e)
        {
            if (m_all_problems_lst.SelectedIndex < 0)
                return;
            String selected = (String)m_all_problems_lst.SelectedItem;
            if (! this.m_selected_problems_lst.Items.Contains(selected)) 
               this.m_selected_problems_lst.Items.Add(selected);
            //this.m_all_problems_lst.Items.Remove(selected);
        }

        private void m_remove_problem_button_Click(object sender, EventArgs e)
        {
            if (m_selected_problems_lst.SelectedIndex < 0)
                return;
            String selected = (String)m_selected_problems_lst.SelectedItem;
            this.m_selected_problems_lst.Items.Remove(selected);
        }

        private void m_refresh_list_button_Click(object sender, EventArgs e)
        {
            updateProblemList();
        }

        private void updateProblemList()
        {
            m_all_problems_lst.Items.Clear();
            List<String> prNameList = ApplicManager.Instance.getProblemListFromDB();
            foreach (String name in prNameList)
            {
                this.m_all_problems_lst.Items.Add(name);
            }
        }
    }
}
